package com.cgah

import grails.test.*
import com.cgah.Bid
import groovy.mock.interceptor.MockFor
import groovy.mock.interceptor.StubFor
import org.apache.ivy.core.resolve.IvyNodeCallers.Caller
import com.cgah.BidController
import com.cgah.CustomerController
import com.cgah.Customer

class CustomerControllerTests extends GrailsUnitTestCase {

    def session
    def params
    def redirectParams
    def flash

    /** Setup metaclass fixtures for mocking.  */
    void setUp() {
        super.setUp()
        session = [:]
        CustomerController.metaClass.getSession = {-> session }

        params = [id:1]
        CustomerController.metaClass.getParams = {-> params }

        redirectParams = [:]
        CustomerController.metaClass.redirect = { Map args -> redirectParams = args  }

        flash = [:]
        CustomerController.metaClass.getFlash = {-> flash }

        def logger = new Expando(debug: { println it }, info: { println it },
                warn: { println it }, error: { println it })
        CustomerController.metaClass.getLog = {-> logger }
    }

    protected void tearDown() {
        flash = null;
        redirectParams = null;
        params = null;
        session = null;
        super.tearDown()
    }

	/**
	 * C-4: An existing customer can only be deleted through the web interface if they have 0
	 * listings and 0 bids (unit test). The unit test should validate that an appropriate
	 * error message is sent to the view if the customer cannot be deleted because of
	 * associated bids or listings.
	 */
    void testMessageInFlashWhenCustomerHasBidAndOrListing() {
        def mocker =  new MockFor(Customer.class)  // create the Mock support

	    mocker.demand.get { return new Customer() }
        mocker.demand.canDelete() { return false } // used once and throws exception

        mocker.use {                                  // start using the Mock
	        def customerController = new CustomerController()
            customerController.delete()
            assertTrue(flash.message.toString().contains("Delete Failed"));
        }
    }


}
